using System;
using System.IO;

namespace CustomRoad
{
    public enum ScaleMode
    {
        ScaleToNextPoint,
        AutoScale,
        ScaleValue
    }

    public struct RoadModifer
    {
        #region Private Fields

        private const byte VERSION = 0x01;
        private bool mirrorX, createNewUVs;
        private float scale;
        private ScaleMode scaleMode;
        private string shortFilename;
        private float textureScale;

        #endregion Private Fields

        #region Public Constructors

        public RoadModifer(string completeFilename, float scale, bool mirrorX, bool createNewUVs, float textureScale, ScaleMode scaleMode)
        {
            this.shortFilename = completeFilename;
            this.scale = scale;
            this.mirrorX = mirrorX;
            this.createNewUVs = createNewUVs;
            this.scaleMode = scaleMode;
            this.textureScale = textureScale;
        }

        #endregion Public Constructors

        #region Public Properties

        public bool CreateNewUVs
        {
            get { return createNewUVs; }
            set { createNewUVs = value; }
        }

        public bool MirrorX
        {
            get { return mirrorX; }
            set { mirrorX = value; }
        }

        public float Scale
        {
            get { return scale; }
            set { scale = value; }
        }

        public ScaleMode ScaleMode
        {
            get { return scaleMode; }
            set { scaleMode = value; }
        }

        public string ShortFilename
        {
            get { return shortFilename; }
            set { shortFilename = value; }
        }

        public float TextureScale
        {
            get { return textureScale; }
            set { textureScale = value; }
        }

        #endregion Public Properties

        #region Public Methods

        public override string ToString()
        {
            return Path.GetFileNameWithoutExtension(ShortFilename);
        }

        #endregion Public Methods

        #region Internal Methods

        internal static RoadModifer FromStream(BinaryReader br)
        {
            switch (br.ReadByte())
            {
                case 0x01: // Version 1
                    return LoadV1(br);
            }
            throw new NotImplementedException();
        }

        internal void ToStream(BinaryWriter bw)
        {
            bw.Write(VERSION);
            bw.Write(this.shortFilename);
            bw.Write((short)this.scaleMode);
            bw.Write(this.scale);
            bw.Write(this.createNewUVs);
            bw.Write(this.textureScale);
            bw.Write(this.mirrorX);
        }

        #endregion Internal Methods

        #region Private Methods

        private static RoadModifer LoadV1(BinaryReader br)
        {
            RoadModifer roadModifer = new RoadModifer();

            roadModifer.shortFilename = br.ReadString();
            roadModifer.scaleMode = (ScaleMode)br.ReadInt16();
            roadModifer.scale = br.ReadSingle();
            roadModifer.createNewUVs = br.ReadBoolean();
            roadModifer.textureScale = br.ReadSingle();
            roadModifer.mirrorX = br.ReadBoolean();

            return roadModifer;
        }

        #endregion Private Methods
    }
}